package com.itheima.reggie.service.impl;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.mapper.ShoppingCartMapping;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;

    @Autowired(required = false)
    private ShoppingCartMapping shoppingCartMapping;


    @Transactional
    @Override
    public R<String> save(Orders orders,Long userId) {
        Long uuid = new Long(UUIDUtils.getUUIDInOrderId());

        orders.setId(uuid);
        orders.setNumber(uuid+"");
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());

        AddressBook addressBook = addressBookMapper.selectById(orders);
        orders.setPhone(addressBook.getPhone());
        orders.setConsignee(addressBook.getConsignee());
        orders.setAddress(addressBook.getDetail());

        //实收金额
        List<ShoppingCart> shoppingCartList = shoppingCartMapping.findAllByUserId(userId);

        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();
        BigDecimal sum = new BigDecimal("0");
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setId(new Long(UUIDUtils.getUUIDInOrderId()));
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setOrderId(new Long(orders.getNumber()));
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setAmount(new BigDecimal(shoppingCart.getNumber()));
            orderDetail.setAmount(shoppingCart.getAmount());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetailList.add(orderDetail);
            sum = sum.add(new BigDecimal(shoppingCart.getAmount() + "").multiply(new BigDecimal(shoppingCart.getNumber())));
        }

        orders.setAmount(sum);

        orderMapper.save(orders);
        orderDetailMapper.save(orderDetailList);
        shoppingCartMapping.deleteByUserId(userId);

        return R.success("新增成功");

    }
}
